﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using DLL;
using ENUMS;

namespace BLL
{
    /// <summary>
    /// 登陆成功后获取的信息
    /// </summary>
    public struct landInfo
    {
        private identity _ident;
        private string _userId;
        private string _userName;

        /// <summary>
        /// 身份
        /// </summary>
        public identity Ident
        {
            get { return _ident; }
            set { _ident = value; }
        }

        /// <summary>
        /// 姓名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        /// <summary>
        /// 身份识别码（ID或学号）
        /// </summary>
        public string UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }
    };

    public static class landing_BLL
    {
        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="name">账号</param>
        /// <param name="pwd">密码</param>
        /// <param name="ident">身份</param>
        public static landInfo ValiUser(string name, string pwd, identity ident)
        {
            //创建用于记录验证成功后的信息的对象
            landInfo info = new landInfo();
            //实例化数据库
            dbExamDataContext db = new dbExamDataContext();
            //创建一个员工对象
            ZG zg;
            //根据身份验证登陆
            switch (ident)
            {
                //工作人员登陆
                case identity.worker:
                    try
                    {
                        zg = db.ZG.Single(p => p.zgName == name && p.zgPwd == pwd);
                        /*如果验证成功则将关键用户信息记录下来*/
                        //记录用户ID
                        info.UserId = zg.zgId.ToString();
                        //记录用户姓名
                        info.UserName = zg.zgName;
                        /*记录用户身份*/
                        //是管理员
                        if ((bool)zg.bMana)
                            info.Ident = identity.manager;
                        //非管理员
                        else
                            info.Ident = identity.counsellor;
                    }
                    catch { }
                    break;

                //学生登陆验证
                case identity.student:
                    try
                    {
                        XS xs = db.XS.Single(p => p.xsId == name && p.xsIdCard == pwd);
                        /*如果验证成功则将关键用户信息记录下来*/
                        //记录学生学号
                        info.UserId = xs.xsId;
                        //记录学生身份
                        info.Ident = identity.student;
                        //记录学生姓名
                        info.UserName = xs.xsName;
                    }
                    catch { }
                    break;
            }
            //返回验证结果
            return info;
        }
    }
}
